package com.linln.wtp.controller.vehicle;

import com.linln.common.utils.DateUtil;
import com.linln.common.utils.SessionUtil;
import com.linln.common.vo.JsonResult;
import com.linln.component.wtpshiro.annotation.UserLoginToken;
import com.linln.modules.vehicle.domain.Vehicle;
import com.linln.modules.vehicle.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * @author : pengliang
 * @version V1.0
 * @Project: timo
 * @Package com.linln.wtp.controller.vehicle
 * @Descriptio /**
 * @Project: timo
 * @Package com.linln.wtp.controller.vehicle
 * @Description: TODO
 * @date Date : 2020年12月18日 11:52
 */

@RestController
@RequestMapping("vehicle")
public class VehicleController {
    @Autowired
    private VehicleService vehicleService;

    @PostMapping
    public JsonResult addVehicles(Vehicle vehicle) {
        try {
            vehicle.setUserId(SessionUtil.getUserId());
            if(vehicle.getEngineNo().length()<6){
                return JsonResult.error("车架号必须大于6位");
            }
            if(vehicle.getVinNo().length()<6){
                return JsonResult.error("VIN必须大于6位");
            }
            if(DateUtil.compareToDate(DateUtil.parseDate(vehicle.getLatelyYearlyInspectionDate(),"yyyy-MM-dd"),new Date()) > 0){
                return JsonResult.error("年审时间不能大于当前时间");
            }
            if(DateUtil.compareToDate(DateUtil.parseDate(vehicle.getRegDate(),"yyyy-MM-dd"),new Date()) > 0){
                return JsonResult.error("注册时间不能大于当前时间");
            }
            return vehicleService.addVehicle(vehicle);
        } catch (Exception e) {
            return JsonResult.error("添加失败");
        }
    }

    @GetMapping
    public Map<String, Object> getVehicles() {
        Map<String, Object> map;
        map = vehicleService.getVehicleList(SessionUtil.getUserId());
        map.put("code", 200);
        return map;
    }

    @DeleteMapping
    public Map<String, Object> delVehicles(String vehicleId) {
        Map<String, Object> map;
        try {
            if (vehicleService.delVehicle(vehicleId)) {
                map = vehicleService.getVehicleList(SessionUtil.getUserId());
                map.put("msg", "删除成功");
                map.put("code", 200);
                return map;
            } else {
                return JsonResult.error("删除失败").put("code", 500);
            }
        } catch (Exception e) {
            return JsonResult.error("删除失败").put("code", 500);
        }
    }

    @GetMapping("/insurance")
    @UserLoginToken
    public Map<String, Object> getInsuranceVehicles() {
        Map<String, Object> map;
        map = vehicleService.getInsuranceVehicles(SessionUtil.getUserId());
        map.put("code", 200);
        return map;
    }
}

